<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Rift Explorer</title>
        <link rel="icon" type="image/png" href="images/favicon-32x32.png" sizes="32x32" />
        <link rel="icon" type="image/png" href="images/favicon-16x16.png" sizes="16x16" />
        <link href='css/typography.css' media='screen' rel='stylesheet' type='text/css'/>
        <link href='css/reset.css' media='screen' rel='stylesheet' type='text/css'/>
        <link href='css/screen.css' media='screen' rel='stylesheet' type='text/css'/>
        <link href='css/reset.css' media='print' rel='stylesheet' type='text/css'/>
        <link href='css/print.css' media='print' rel='stylesheet' type='text/css'/>
        <link href='css/style.css' rel='stylesheet' type='text/css'/>
        <link href='css/code-theme.css' rel='stylesheet' type='text/css'/>
        <script src='lib/jquery-1.8.0.min.js' type='text/javascript'></script>
        <script src='lib/jquery.slideto.min.js' type='text/javascript'></script>
        <script src='lib/jquery.wiggle.min.js' type='text/javascript'></script>
        <script src='lib/jquery.ba-bbq.min.js' type='text/javascript'></script>
        <script src='lib/handlebars-2.0.0.js' type='text/javascript'></script>
        <script src='lib/underscore-min.js' type='text/javascript'></script>
        <script src='lib/backbone-min.js' type='text/javascript'></script>
        <script src='swagger-ui.js' type='text/javascript'></script>
        <script src='lib/highlight.9.14.2.min.js' type='text/javascript'></script>
        <script src='lib/jsoneditor.min.js' type='text/javascript'></script>
        <script src='lib/marked.js' type='text/javascript'></script>
        <script src='lib/swagger-oauth.js' type='text/javascript'></script>

    </head>

    <body class="swagger-section">
        <div id="message-bar" class="swagger-ui-wrap" data-sw-translate>Waiting for League to start...</div>
        <div id="swagger-ui-container" class="swagger-ui-wrap"></div>

        <script type="text/javascript">
            const IPC = require('electron').ipcRenderer;
            const shell = require('electron').shell;
            const pkg = require('../package');

            const swaggerContainer = document.querySelector('#swagger-ui-container');
            const messageBar = document.querySelector('#message-bar');

            let LCUData = null;

            // We all love global variables
            window.RE = {
                info: {
                    title: 'Rift Explorer',
                    description: pkg.description,
                    version: pkg.version,
                }
            };

            const loadSwagger = () => {
                const { username, password, address, port } = LCUData;
                const header = `Basic ${btoa(`${username}:${password}`)}`;
                const auth = new SwaggerClient.ApiKeyAuthorization("Authorization", header, "header");

                window.swaggerUi = new SwaggerUi({
                    validatorUrl: null,
                    url: `https://${username}:${password}@${address}:${port}/swagger/v2/swagger.json`,
                    authorizations: { riot: auth },
                    dom_id: "swagger-ui-container",
                    onComplete() {
                        document.querySelectorAll('pre code').forEach((block) => {
                            hljs.highlightBlock(block);
                        });

                        window.swaggerUi.api.clientAuthorizations.add("riot", auth);
                    },
                    // Failure has been taken care of in the swagger-ui file
                    docExpansion: "none",
                    jsonEditor: false,
                    apisSorter: "alpha",
                    defaultModelRendering: 'schema',
                    showRequestHeaders: true
                });

                window.swaggerUi.load();
            };

            IPC.on('lcu-load', (event, data) => {
                LCUData = data;
                loadSwagger();
            });

            IPC.on('lcu-unload', () => {
                LCUData = null;
                swaggerContainer.innerHTML = '';
                messageBar.innerHTML = 'Waiting for League to start...';
            });

            function _handleGithubLink(event) {
                shell.openExternal('https://github.com/pupix/rift-explorer');
            }

            function _handleDiscordLink(event) {
                shell.openExternal('https://discord.gg/hPtrMcx');
            }
        </script>
    </body>
</html>
